<?php

namespace IMooc;

/**
 * 单例模式
 */
class Singleton
{
    // PHP 7.4以后增加的类型化类属性
    protected static ?object $instance = null;

    protected static int $age = 18;

    private function __construct()
    {

    }

    static function getInstance(): Singleton
    {
        if (!self::$instance) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    static function addAge(): void
    {
        self::$age += 1;
    }

    static function getAge(): int
    {
        return self::$age;
    }
}